import React, { Component } from 'react';
import NetUitl from '../../../utils/HttpUtil';
import {Button ,message} from "antd";
import AddTeam from "../../../components/AddTeam";
import AddSaturation from "../../../components/AddSaturation";
import AddOther from "../../../components/AddOther";
import AddTask from "../../../components/AddTask";
import NoteModal from '../../../components/NoteModal'

import "./addManagerun.scss";
import dataColor from "echarts/src/visual/dataColor";

let checkFn=(data)=>{
    let team=data.team
    let other=data.other;
    let isgo = true
    console.log(team.attendanceNum + team.absenteeismNum)
   
    if(team.teamPeopleNum !=( Number(team.attendanceNum) + Number(team.absenteeismNum))){
        message.warning("团队人数不等于‘出勤人数’+‘缺勤人数’之和")
        isgo = false
    }
    if(team.attendanceNum  !=( Number(other.companyOfficeNum)+ Number(other.homeOfficeNum) +Number(other.clientOfficeNum))){
        message.warning("出勤人数不等于‘公司办公人数+居家办公人数+客户现场人数‘之和")
        isgo = false
    }
    if(team.absenteeismNum !=(Number(other.vacationNum) + Number(other.minerNum))){
        message.warning("出勤人数不等于‘休假人数+旷工人数‘之和")
        isgo = false
    }
    return isgo
}
class AddManagerunPage extends Component {
    constructor(props) {
        super(props);
        this.state = {
            id:'',
            date:'',
            stateType:1,//1新增，2修改，3显示
            allData:{
                top:{
                    teamName:"",
                    reportDate:"",
                },
                team:{
                    personOnDuty:"",
                    phoneOnDuty:"",
                    teamId:"",
                    teamName:"",
                    teamType:"",
                    teamLeader:"",
                    teamPeopleNum:"",//团队人数
                    attendanceNum:"",//出勤人事
                    attendancePercent:"",
                    absenteeismNum:"",//缺勤人数
                    absenteeismPercent:""
                },
                sat:{
                    codeNum:"",
                    updateDemandNum:"",
                    updateBugNum:0,
                    codeAvg:"",
                    updateDemandAvg:"",
                    updateBugAvg:0
                },
                other:{
                    moduleNum:"",
                    supportProjectNum:"",
                    companyOfficeNum:"",//公司办公人数不能为空！
                    homeOfficeNum:"",
                    clientOfficeNum:"",
                    vacationNum:"",
                    minerNum:""
                },
                task:[
                    {
                        module:"",
                        progress:"",
                        content:"",
                        project:"",
                        openProblemNum:"",
                        problemProjectNum:"",
                        supportPeopleNum:""
                    }
                ],
                remark:'',
            },
            noteVisible:false
        }
    }

    componentWillMount(){
        const {location} = this.props
        let  query =location ?location.query:null;
        if(!query){
            this.handlecancel()
        }else{
            let isAdd = query.add
            let id = query.teamId
            let cid = query.id
            let date = query.reportDate && query.reportDate? query.reportDate:""
            console.log(query,"query")

            if (query && !isAdd ){
                console.log("======編輯=======");
                let url = `dailyReport/getReportDetail?id=${cid}`;
                let self = this;
                NetUitl.get(url, null ,function (data) {
                    if (data && data.msgCode == 200) {
                        let allData = data.data
                        allData["team"].teamName=allData["top"].teamName
                        self.setState({allData:allData,stateType:2,id:id,date:date})
                    }else{
                    }
                }, function (data) {
                    console.log(data)
                })
            }else{
                console.log("======新增=======");
                let allData= this.state.allData;
                let top ={};
                top["teamName"] = this.props.location.query.teamName;
                
                top.reportDate = this.props.location.query.reportDate;
                allData.top = top;
                allData.team.teamId = this.props.location.query.teamId;
                allData["team"].teamName=allData["top"].teamName

                this.setState({allData:allData,stateType:1,id:id,date:date})
                console.log(allData)
            }
        }
    }

    //生成保存函數
    handleCreate=()=>{
        console.log("生成数据")
        console.log(this.state.allData)
        // let allData = this.state.allData
        
        let {date,id,stateType,allData}=this.state;
        let team = allData.team;
        if (team.personOnDuty === "" || team.personOnDuty === undefined || team.personOnDuty=== null){
            return message.warning("本日值班不能为空！")
        }
        if (team.phoneOnDuty === "" || team.phoneOnDuty === undefined || team.phoneOnDuty=== null){
            return message.warning("联系电话不能为空！")
        }
        if (team.teamType === "" || team.teamType === undefined || team.teamType=== null){
            return message.warning("团队类型不能为空！")
        }
        if (team.teamLeader === "" || team.teamLeader === undefined || team.teamLeader=== null){
            return message.warning("团队负责人不能为空！")
        }
        if (team.teamPeopleNum === "" || team.teamPeopleNum === undefined || team.teamPeopleNum=== null){
            return message.warning("团队人数不能为空！")
        }
        if (team.attendanceNum === "" || team.attendanceNum === undefined || team.attendanceNum=== null){
            return message.warning("出勤人数不能为空！")
        }
        let sat = allData.sat;
        if (sat.codeNum === "" || sat.codeNum === undefined || sat.codeNum=== null){
            return message.warning("当日代码行数不能为空！")
        }
        if (sat.updateDemandNum === "" || sat.updateDemandNum === undefined || sat.updateDemandNum=== null){
            return message.warning("当日修改需求数不能为空！")
        }
        if (sat.updateBugNum === "" || sat.updateBugNum === undefined || sat.updateBugNum === null){
            return message.warning("当日修改BUG数不能为空！")
        }
        let other = allData.other;
        if (other.moduleNum === "" || other.moduleNum === undefined || other.moduleNum === null){
            return message.warning("在研模块数不能为空！")
        }
        if (other.supportProjectNum === "" || other.supportProjectNum === undefined || other.supportProjectNum === null){
            return message.warning("支持项目数不能为空！")
        }
        if (other.companyOfficeNum === "" || other.companyOfficeNum === undefined || other.companyOfficeNum === null){
            return message.warning("公司办公人数不能为空！")
        }
        if (other.homeOfficeNum === "" || other.homeOfficeNum === undefined || other.homeOfficeNum === null){
            return message.warning("居家办公人数不能为空！")
        }
        if (other.clientOfficeNum === "" || other.clientOfficeNum === undefined || other.clientOfficeNum === null){
            return message.warning("客户现场人数不能为空！")
        }
        if (other.vacationNum === "" || other.vacationNum === undefined || other.vacationNum === null){
            return message.warning("休假人数不能为空！")
        }
        if (other.minerNum === "" || other.minerNum === undefined || other.minerNum === null){
            return message.warning("旷工人数不能为空！")
        }
        if(!checkFn(allData)){
            return
        }
        let url = `dailyReport/saveOrUpdate`
        let self = this;

        allData["team"].teamName=allData["top"].teamName
        if(this.refs && this.refs.AddTaskfn && this.refs.AddTaskfn.state){
            console.log("生成數據",this.refs.AddTaskfn.state.dataSource)
            // if(this.refs.AddTaskfn.state.iscopy){
                allData["task"]=this.refs.AddTaskfn.state.dataSource
            // }
        }
        // if(stateType==1){
        //     allData["task"].map((item,i)=>{

        //     })
        // }
        NetUitl.post(url, allData, function (data) {
            let url = `/index.html#/result?id=${id}&date=${date}`
            if (data && data.msgCode == 200) {
                console.log(data.data)
                window.location.href=url
            }else if(data.msgCode==202){
                // alert(data.msg)
                message.error(data.msg)
            }else{
                message.error("后台报错")
            }
        }, function (data) {
            message.error("后台报错")
        })
    }

    //返回主页
    handlecancel=()=>{
        window.location.href="/index.html#/"
    }

    handleNote = () =>{
        const {allData} = this.state

        if(this.refs && this.refs.AddTaskfn && this.refs.AddTaskfn.state){
            console.log("生成數據",this.refs.AddTaskfn.state.dataSource)
            // if(this.refs.AddTaskfn.state.iscopy){
                allData["task"]=this.refs.AddTaskfn.state.dataSource
            // }
        }
        console.log(allData,"all")
        const testData = {};
        testData.allData = allData
        this.setState({
            noteVisible:true,
            allData
        })
    }
    handleNoteOK = (v) =>{
        const {allData} = this.state
        allData.remark = v

 
        this.setState({
            noteVisible:false,
            allData
        })
    }
    handleNoteCancel = (v) =>{
        this.setState({
            noteVisible:false
        })
    }

    render() {
        let {allData,date,id,stateType,noteVisible} = this.state;
        let { location} =this.props;
        const teamName = location && location.query &&  location.query.add ===true ? this.props.location.query.teamName : allData.top.teamName;
        const reportDate = location && location.query &&  location.query.add ===true ? this.props.location.query.reportDate :allData.top.reportDate;
        console.log(this.state,"render")

        return (
            <div className="add-box">
                <div className="add-top">
                    <h3>{teamName}</h3>
                    <span>{reportDate}</span>
                    <span>填完信息请点击“生成”将保存页面信息并跳转到结果页面；点击“返回”不保存信息直接放回列表页面</span>
                    <Button type="primary" onClick={this.handleCreate} >生成</Button>
                    <Button onClick={this.handleNote} style={{marginRight:8}}>备注</Button>
                    <Button onClick={this.handlecancel} style={{marginRight:8}}>返回</Button>

                </div>
                <div className={"addFloor-main"}>
                    <div className={"add-floor2"}>
                        <AddTeam isAdd={true}  allData={allData} viewer={this} 
                         date={date}
                         id={id}
                         stateType={stateType}
                        />
                    </div>
                    <div className={"add-floor3"}>
                        <AddSaturation  isAdd={true} allData={allData}  viewer={this} 
                         date={date}
                         id={id}
                         stateType={stateType}
                        />

                    </div>
                    <div className={"add-floor4"}>
                        <AddOther isAdd={true} allData={allData}  
                        viewer={this}
                        date={date}
                        id={id}
                        stateType={stateType}
                        />
                    </div>
                    <div className={"add-floor5"}>
                        <AddTask isAdd={true} 
                        allData={allData} 
                         viewer={this} 
                         date={date}
                         id={id}
                         stateType={stateType}
                         ref="AddTaskfn"
                          />
                    </div>
                </div>
                <NoteModal
                    title={'备注'}
                    noteVisible={noteVisible}
                    allData={allData}
                    handleNoteOK={this.handleNoteOK}
                    handleNoteCancel={this.handleNoteCancel}
                />
            </div>
        );
    }
}

export default AddManagerunPage
